describe('practice-7-7',function(){
    it('找出计算结果为24的表达式',function(){
        var arr_brackets_one = [0,1,5,8];
        var arr_brackets_two = [0,3,7,8];
        var arr_brackets_thr = [2,5,9,10];
        var arr_brackets_fou = [2,3,7,10];
        var arr_brackets_fiv = [0,4,6,10];
        var array = [arr_brackets_one,arr_brackets_two,arr_brackets_thr,arr_brackets_fou,arr_brackets_fiv];
        var arr_four_five = [[3,6,9],[2,5,9],[1,4,7],[1,5,8],[2,5,8]];

        var arr_result = twenty_four_game(1,2,3,4);
        expect(arr_result.length).toBe(7);
        for(var i = 0; i < arr_result.length; i ++)
        {
            var expression = arr_result[i];
            var value = get_type_of_five(expression,array);
            //计算这种表达式
            var res_tmp = get_result(expression,arr_four_five,value,1,2,3,4);
            console.info(res_tmp);
            expect(res_tmp).toBe(24);
        }
    });
});
function get_type_of_five(expression,array)
{
    var arr_temp = [];
    var value;
    for(var j = 0; j < expression.length; j ++)
    {
        if(expression.substr(j,1) == '(' || expression.substr(j,1) == ')')
        {
            arr_temp.push(j);
        }
    }
    //找出是哪种表达式
    for(var m = 0; m < array.length; m ++)
    {
        var count = 0;
        for(var n = 0; n < 4; n ++)
        {
            if(array[m][n] === arr_temp[n])
                count ++;
        }
        if(count === 4)
        {
            value = m;
            break;
        }
    }
    return value;
}
function cal(type,x,y)
{
    switch (type)
    {
        case '+':
            return x+y;
        case '-':
            return x-y;
        case '*':
            return x*y;
        case '/':
            return x/y;
    }
}

function get_result(expression,arr_four_five,m,a,b,c,d)
{
    var res_tmp = 0;
    var flag1,flag2,flag3;
    if(m===0)
    {

        flag1 = expression.substr(arr_four_five[m][0],1);
        res_tmp = cal(flag1,a,b);
        flag2 = expression.substr(arr_four_five[m][1],1);
        res_tmp = cal(flag2,res_tmp,c);
        flag3 = expression.substr(arr_four_five[m][2],1);
        res_tmp = cal(flag3,res_tmp,d);
    }
    else if(m===1)
    {
        flag1 = expression.substr(arr_four_five[m][1],1);
        res_tmp = cal(flag1,b,c);
        flag2 = expression.substr(arr_four_five[m][0],1);
        res_tmp = cal(flag2,a,res_tmp);
        flag3 = expression.substr(arr_four_five[m][2],1);
        res_tmp = cal(flag3,res_tmp,d);
    }
    else if(m===2)
    {
        flag1 = expression.substr(arr_four_five[m][2],1);
        res_tmp = cal(flag1,c,d);
        flag2 = expression.substr(arr_four_five[m][1],1);
        res_tmp = cal(flag2,b,res_tmp);
        flag3 = expression.substr(arr_four_five[m][0],1);
        res_tmp = cal(flag3,a,res_tmp);
    }
    else if(m===3)
    {
        flag1 = expression.substr(arr_four_five[m][1],1);
        res_tmp = cal(flag1,b,c);
        flag2 = expression.substr(arr_four_five[m][2],1);
        res_tmp = cal(flag2,res_tmp,d);
        flag3 = expression.substr(arr_four_five[m][0],1);
        res_tmp = cal(flag3,a,res_tmp);
    }
    else if(m===4)
    {
        flag1 = expression.substr(arr_four_five[m][0],1);
        res_tmp = cal(flag1,a,b);
        flag2 = expression.substr(arr_four_five[m][2],1);
        var res_tmp_2 = cal(flag2,c,d);
        flag3 = expression.substr(arr_four_five[m][1],1);
        res_tmp = cal(flag3,res_tmp,res_tmp_2);
    }
    return res_tmp;
}